package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.PrcGroup;
import com.colorlight.cloud.complete.entity.po.ScreenLock;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取屏幕冻结状态
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetScreenLock implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送卡屏幕信息
         */
        private List<PrcGroup> prcGroup;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 屏幕冻结状态
         */
        private List<ScreenLock> screenLock;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.GetScreenLock.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.GetScreenLock.InputMsg.newBuilder();
        GetScreenLock.InputMsg thisInput = this.getInputMsg();
        inputMsgBuilder.addAllPrcGroup(thisInput.getPrcGroup().stream().map(PrcGroup::toProto).collect(Collectors.toList()));

        final CompleteInstruction.GetScreenLock.Builder builder =
                CompleteInstruction.GetScreenLock.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public GetScreenLock byteConvertObject(ByteString bytes) {
        CompleteInstruction.GetScreenLock getScreenLock = null;
        try {
            getScreenLock = CompleteInstruction.GetScreenLock.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(getScreenLock)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        GetScreenLock.OutputMsg outputMsg = new GetScreenLock.OutputMsg();
        CompleteInstruction.GetScreenLock.OutputMsg output = getScreenLock.getOutput();
        List<ScreenLock> collect = output.getScreenLockList().stream().map(e -> new ScreenLock().formProto(e)).collect(Collectors.toList());
        outputMsg.setScreenLock(collect);
        this.setOutputMsg(outputMsg);
        return this;
    }
}

